document.addEventListener('DOMContentLoaded', function() {
  window.onload = function() {
    if (typeof AMap !== 'undefined') {
      var map = new AMap.Map('amap-container', {
        resizeEnable: true,
        center: [116.22, 40.12], // 重庆市九龙坡区半山一村71号附35号附近经纬度
        zoom: 16
      });
      var marker = new AMap.Marker({
        position: [116.22, 40.12],
        title: '北京他山石科技有限公司',
        map: map, // 直接添加到地图
        icon: new AMap.Icon({
          size: new AMap.Size(32, 32), // 图标尺寸
          image: './static/image/marker.png', // 自定义图标路径
          imageSize: new AMap.Size(32, 32) // 图标显示尺寸
        })
      });
      // 可选：添加信息窗体
      var infoWindow = new AMap.InfoWindow({
        content: '<div style="font-size:14px;width: 250px;">北京市海淀区苏州街1号9层018<br>电话: 13501244089(张书稳)<br>邮箱：821440380@qq.com</div>',
        offset:  new AMap.Pixel(15, 0)
      });
      marker.on('click', function() {
        infoWindow.open(map, marker.getPosition());
      });
    }
  }
    // 处理导航栏滚动效果
    window.addEventListener('scroll', function() {
        const navbar = document.querySelector('.navbar');
        if (window.scrollY > 50) {
            navbar.classList.add('bg-white', 'shadow-md');
            navbar.classList.remove('bg-transparent');
        } else {
            navbar.classList.add('bg-white');
            navbar.classList.remove('shadow-md', 'bg-transparent');
        }
    });

    // 移动端导航菜单控制
    const navbarToggler = document.querySelector('.navbar-toggler');
    const navbarCollapse = document.getElementById('navbarNav');
    
    navbarToggler.addEventListener('click', function() {
        navbarCollapse.classList.toggle('show');
    });

    // 处理关于我们的弹窗交互
    const aboutUsBtn = document.getElementById('aboutUsContainer');
    const aboutUsDropdown = document.getElementById('aboutUsDropdown');

    // 确保弹窗在移动端可以正常显示
    const isMobile = window.innerWidth <= 768;
    if (isMobile) {
        aboutUsBtn.addEventListener('click', function(e) {
            // 只有在点击'关于我们'文本时才触发下拉菜单，避免影响链接跳转
            if (e.target === aboutUsBtn || e.target === aboutUsBtn.querySelector('.nav-link')) {
                e.preventDefault();
                aboutUsDropdown.classList.toggle('show');
            }
        });
    }
    
    // 监听关于我们下拉菜单中的点击事件
    document.querySelectorAll('#aboutUsDropdown .dropdown-item').forEach(item => {
        item.addEventListener('click', function() {
            // 确保点击后能正常跳转到相应页面
            if (this.textContent.includes('合作伙伴')) {
                this.setAttribute('href', 'certificates.html?type=partners');
            } else if (this.textContent.includes('荣誉证书')) {
                this.setAttribute('href', 'certificates.html?type=certificates');
            }
            
            // 在非移动设备上，我们可以手动隐藏下拉菜单
            if (!isMobile) {
                aboutUsDropdown.classList.remove('show');
            }
        });
    });



    // 移除阻止导航链接默认行为的代码
    // 移动端导航菜单关闭处理
    document.querySelectorAll('.nav-link, .dropdown-item').forEach(link => {
        link.addEventListener('click', function() {
            if (navbarCollapse.classList.contains('show')) {
                navbarToggler.click();
            }
        });
    });

    // 为业务表单添加提交事件监听
    document.getElementById('businessFormContent').addEventListener('submit', function(event) {
        event.preventDefault();
        validateAndSubmitBusinessForm();
    });
    
    // 为招贤纳士表单添加提交事件监听
    document.getElementById('careersFormContent').addEventListener('submit', function(event) {
        event.preventDefault();
        validateAndSubmitCareersForm();
    });
    
    // 初始化业务往来按钮为激活状态
    document.getElementById('businessBtn').classList.add('active');
});

// 单选按钮逻辑
function selectButton(buttonId) {
    // 先取消所有按钮的选中状态
    document.querySelectorAll('.business-button').forEach(btn => {
        btn.classList.remove('active');
    });
    // 设置当前按钮为选中状态
    document.getElementById(buttonId).classList.add('active');
    
    // 根据选中的按钮显示对应的表单
    if (buttonId === 'businessBtn') {
        // 显示业务往来表单
        document.getElementById('businessForm').style.display = 'block';
        document.getElementById('careersForm').style.display = 'none';
        // 清空业务表单
        clearBusinessForm();
        // 更新表单左侧业务类型文本
        updateActiveBusinessType('业务往来', '有任何问题吗？<br>我们很乐意听取您的意见。');
    } else if (buttonId === 'careersBtn') {
        // 显示招贤纳士表单
        document.getElementById('businessForm').style.display = 'none';
        document.getElementById('careersForm').style.display = 'block';
        // 清空招贤纳士表单
        clearCareersForm();
        // 更新表单左侧业务类型文本
        updateActiveBusinessType('招贤纳士', '加入我们的团队<br>帮助我们维持更美好的明天。');
    }
}

// 更新表单左侧激活的业务类型文本
function updateActiveBusinessType(title, subtitle) {
    document.querySelector('.business-type-title').textContent = title;
    document.querySelector('.business-type-subtitle').innerHTML = subtitle;
}

// 清空业务表单
function clearBusinessForm() {
    document.getElementById('name').value = '';
    document.getElementById('phone').value = '';
    document.getElementById('email').value = '';
    document.getElementById('company').value = '';
    document.getElementById('requirement').value = '';
    document.getElementById('contactPhone').checked = true;
}

// 清空招贤纳士表单
function clearCareersForm() {
    document.getElementById('careersName').value = '';
    document.getElementById('careersPhone').value = '';
    document.getElementById('careersEmail').value = '';
    document.getElementById('jobIntention').value = '';
    document.getElementById('careersContactPhone').checked = true;
}

// 验证并提交业务表单
function validateAndSubmitBusinessForm() {
    const name = document.getElementById('name').value.trim();
    const phone = document.getElementById('phone').value.trim();
    const email = document.getElementById('email').value.trim();
    const company = document.getElementById('company').value.trim();
    const requirement = document.getElementById('requirement').value.trim();
    
    // 判空验证
    if (!name) {
        alert('请输入姓名');
        return;
    }
    if (!phone) {
        alert('请输入电话');
        return;
    }
    if (!email) {
        alert('请输入邮箱');
        return;
    }
    if (!company) {
        alert('请输入公司名称');
        return;
    }
    if (!requirement) {
        alert('请描述业务需求');
        return;
    }
    
    // 验证手机号码格式
    const phoneRegex = /^1[3-9]\d{9}$/;
    if (!phoneRegex.test(phone)) {
        alert('请输入正确的手机号码');
        return;
    }
    
    // 验证邮箱格式
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    if (!emailRegex.test(email)) {
        alert('请输入正确的邮箱地址');
        return;
    }
    
    // 显示成功弹窗
    showSuccessToast('提交成功！');
    
    // 清空表单
    clearBusinessForm();
}

// 验证并提交招贤纳士表单
function validateAndSubmitCareersForm() {
    const name = document.getElementById('careersName').value.trim();
    const phone = document.getElementById('careersPhone').value.trim();
    const email = document.getElementById('careersEmail').value.trim();
    const jobIntention = document.getElementById('jobIntention').value.trim();
    
    // 判空验证
    if (!name) {
        alert('请输入姓名');
        return;
    }
    if (!phone) {
        alert('请输入电话');
        return;
    }
    if (!email) {
        alert('请输入邮箱');
        return;
    }
    if (!jobIntention) {
        alert('请描述求职意向');
        return;
    }
    
    // 验证手机号码格式
    const phoneRegex = /^1[3-9]\d{9}$/;
    if (!phoneRegex.test(phone)) {
        alert('请输入正确的手机号码');
        return;
    }
    
    // 验证邮箱格式
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    if (!emailRegex.test(email)) {
        alert('请输入正确的邮箱地址');
        return;
    }
    
    // 显示成功弹窗
    showSuccessToast('提交成功！');
    
    // 清空表单
    clearCareersForm();
}

// 使用Bootstrap的toast组件显示成功提示
function showSuccessToast(message) {
    // 检查是否已存在toast元素
    let toastElement = document.getElementById('successToast');
    
    if (!toastElement) {
        // 创建toast容器
        const toastContainer = document.createElement('div');
        toastContainer.className = 'toast-container position-fixed top-50 start-50 translate-middle';
        toastContainer.style.zIndex = '9999';
        
        // 创建toast元素
        toastElement = document.createElement('div');
        toastElement.id = 'successToast';
        toastElement.className = 'toast align-items-center text-bg-success border-0';
        toastElement.setAttribute('role', 'alert');
        toastElement.setAttribute('aria-live', 'assertive');
        toastElement.setAttribute('aria-atomic', 'true');
        
        // toast内容
        const toastContent = document.createElement('div');
        toastContent.className = 'd-flex';
        
        const toastBody = document.createElement('div');
        toastBody.className = 'toast-body';
        toastBody.textContent = message;
        
        const toastClose = document.createElement('button');
        toastClose.type = 'button';
        toastClose.className = 'btn-close btn-close-white me-2 m-auto';
        toastClose.setAttribute('data-bs-dismiss', 'toast');
        toastClose.setAttribute('aria-label', 'Close');
        
        // 组装toast元素
        toastContent.appendChild(toastBody);
        toastContent.appendChild(toastClose);
        toastElement.appendChild(toastContent);
        toastContainer.appendChild(toastElement);
        
        // 添加到页面
        document.body.appendChild(toastContainer);
    } else {
        // 更新消息内容
        toastElement.querySelector('.toast-body').textContent = message;
    }
    
    // 显示toast
    const toast = new bootstrap.Toast(toastElement, {
        delay: 3000, // 3秒后自动关闭
        autohide: true
    });
    
    toast.show();
}